# -*- coding: utf-8 -*-

'''
    【简介】
	ui转换成py的转换工具
     
'''

import os
import os.path


# UI文件所在的路径
dir = './'


def mkdir(path):
    folder = os.path.exists(path)
    if not folder:
        os.makedirs(path)


# 列出目录下的所有ui文件


def listUiFile(str):
    list = []
    files = os.listdir(dir)
    for filename in files:
        # print( dir + os.sep + f  )
        # print(filename)
        if os.path.splitext(filename)[1] == str:
            list.append(filename)

    return list


def transUiPyFile(filename):
    return os.path.splitext(filename)[0] + '.py'


def transQrcPyFile(filename):
    return os.path.splitext(filename)[0] + '_rc.py'


def ui_to_py():
    list = listUiFile('.ui')
    for uifile in list:
        pyfile = transUiPyFile(uifile)
        cmd = 'pyuic5 -o out_ui_py/{pyfile} {uifile}'.format(
            pyfile=pyfile, uifile=uifile)
        print(cmd)
        os.system(cmd)


def qrc_to_py():
    list = listUiFile('.qrc')
    for qrcfile in list:
        pyfile = transQrcPyFile(qrcfile)
        cmd = 'pyrcc5 -o {pyfile} {qrcfile}'.format(
            pyfile=pyfile, qrcfile=qrcfile)
        print(cmd)
        os.system(cmd)


def runMain():
    file = "./out_ui_py"
    mkdir(file)  # 调用函数
    ui_to_py()
    qrc_to_py()


# 程序的主入口
if __name__ == "__main__":
    runMain()
